package com.cn.jx.manager.controller;

import java.text.MessageFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cn.jx.common.controller.BaseController;
import com.cn.jx.common.util.jpaQuery.ConvertCondition;
import com.cn.jx.common.util.jpaQuery.PageCondition;
import com.cn.jx.common.util.jpaQuery.QueryCondition;
import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.common.vo.SessionUser;
import com.cn.jx.manager.form.OrgForm;
import com.cn.jx.manager.po.Org;
import com.cn.jx.manager.service.mtn.IOrgServiceMtn;
import com.cn.jx.manager.service.query.IOrgServiceQry;
import com.jx.enumType.DataGrideTypeEnum;
import com.jx.enumType.ResourceEnum;
import com.jx.model.MenuEntry;
import com.jx.util.FormValidat;
import com.jx.util.MapObject;
import com.jx.util.Response;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：组织控制层
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Controller
@RequestMapping("/admin/uoj/org/")
public class OrgController  extends BaseController{
	 
	private static final String _PAGE_PATH="userorgjob/";
	
	/**定义{@link IOrgService orgService <tt>组织接口</tt>}*/
	@Autowired
	private IOrgServiceMtn orgServiceMtn;
	
	@Autowired
	private IOrgServiceQry orgServiceQry;
	
	/**
	 * 跳转到页面
	 * @param request
	 * @return
	 */
	@RequestMapping("list/{path}")
	public String userList(Model model, HttpServletRequest request,@PathVariable(value = "path") String path) {
		SessionUser sessUser = getSessionUser(request);
		setModelColumns(model, DataGrideTypeEnum.ORG_MGR);
		setModelPermit(model,ResourceEnum.ORG_MGR,sessUser.getResourceCodeList());
		return MessageFormat.format("{0}{1}", _PAGE_PATH,"recycle".equals(path)?"org/orgrecycle":"orglist");
	}
	 
	/**
	 * 
	* @Title:editWindow
	* @Description:TODO(编辑)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	 @RequestMapping("edit")
	 public String editWindow(Model model,HttpServletRequest request,String id,String parentOrgId,String parentOrgName) {
		 //id存在 则为修改编辑
		 //parentcode 为空则增加根 否则增加子项目
		 Org res= null;
		 if(Utility.isNotBlank(id)){
			 res= orgServiceQry.getOrgById(id);
		 }
		 model.addAttribute("parentOrgName",parentOrgName);
		 model.addAttribute("parentOrgId",parentOrgId);
		 model.addAttribute("res",res);
	     return _PAGE_PATH+"org/orgedit";
	 }
	 

	 /**
	  * 
	 * @Title:orgList
	 * @Description:TODO(组织分页列表)
	 * <p>邮箱地址: 524577360@qq.com
	 * @return
	 * PageObj<Org>
	 * @throws
	  */
	@RequestMapping("listData/{path}")
	@ResponseBody
	public PageObj<MapObject> orgList(Model model,@PathVariable(value = "path") String path,  PageCondition page) {
		QueryCondition cond = ConvertCondition.initCondition(page, path);
		try {
			PageObj<MapObject> pageuser = orgServiceQry.findOrgByPageAndParams(cond);
			return pageuser;
		} catch (Exception e) {
			log.debug(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	 /**
	  * 
	 * @Title:orgTree
	 * @Description:TODO(查询组织树)
	 * <p>邮箱地址: 524577360@qq.com
	 * List<MenuEntry>
	 * @throws
	  */
	@RequestMapping("listTree")
	@ResponseBody
	public List<MenuEntry> orgTree(Model model, HttpServletRequest request, String parent) {
		try {
			List<MenuEntry> result = orgServiceQry.findOrgTree(parent);
			return result;
		} catch (Exception e) {
			log.debug(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	 
	 /**
	  * 
	 * @Title:addOrg
	 * @Description:TODO(添加组织)
	 * <p>邮箱地址: 524577360@qq.com
	 * Response
	 * @throws
	  */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public Response addOrg(Model model, HttpServletRequest request, OrgForm orgForm) {
		SessionUser sessUser = getSessionUser(request);
		// 表单验证
		try {
			FormValidat.exec(orgForm);
			orgForm.setOperatUser(sessUser.getUserId());
			orgServiceMtn.saveOrUpdateOrg(orgForm);
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	 
	 /**
	  * 
	 * @Title:delOrg
	 * @Description:TODO(删除组织根据id，标记删除)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	  */
	 @RequestMapping("delete")
	 @ResponseBody
	 public Response delOrg(Model model,HttpServletRequest request,String ids) {
		SessionUser sessUser= getSessionUser(request);
		 try {
			 FormValidat.validId(ids);
			 //表单验证
			 List<String> list=JSON.parseArray(ids, String.class);
			 orgServiceMtn.delOrg(list,sessUser.getUserId());
			 return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	 }
	 
	 /**
	  * 
	 * @Title:removeOrg
	 * @Description:TODO(彻底移除)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	  */
	@RequestMapping("remove")
	@ResponseBody
	public Response removeOrg(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			// 表单验证
			FormValidat.validId(ids);
			// 表单验证
			List<String> list = JSON.parseArray(ids, String.class);
			orgServiceMtn.removeOrg(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	/**
	 * 
	* @Title:recoverOrg
	* @Description:TODO(找回)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("recover")
	@ResponseBody
	public Response recoverOrg(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			// 表单验证
			FormValidat.validId(ids);
			// 表单验证
			List<String> list = JSON.parseArray(ids, String.class);
			orgServiceMtn.recoverOrg(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	/**
	 * 
	* @Title:removeAllOrg
	* @Description:TODO(移除角色 清空回收站)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("removeAll")
	@ResponseBody
	public Response removeAllOrg(Model model, HttpServletRequest request) {
		SessionUser sessUser = getSessionUser(request);
		try {
			orgServiceMtn.removeOrgAll(sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	 
	 /**
	  * 
	 * @Title:getOrgById
	 * @Description:TODO(根据组织id获取组织信息)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	  */
	@RequestMapping("get")
	@ResponseBody
	public Response getOrgById(Model model, HttpServletRequest request, String id) {
		Org of = orgServiceQry.getOrgById(id);
		return Response.Success(of);
	}
}
